<template>
  <div class="content">
    <div class="filter">
      <el-row>
        <el-col :span="21">
          <headline v-bind:title="title"></headline>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" icon="el-icon-plus" class="tian" @click="tianjia()">添加品类</el-button>
        </el-col>
      </el-row>
      <el-row><h4>当前商品分类ID: {{ this.id }}</h4></el-row>
    </div>
    <div class="data-table">
      <el-table :data="list.slice((pageNum - 1) * pagesize, pageNum * pagesize)" border style="width: 100%"
                max-height="500">
        <el-table-column prop="id" label="品类ID"></el-table-column>
        <el-table-column prop="name" label="品类名称"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="xiugai(scope.row)">修改名称</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[10, 20, 40]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="list.length">
      </el-pagination>
    </div>
    <below v-bind:shwHde="shwHde"></below>
  </div>
</template>
<script>
import headline from "../headline.vue";
import below from "../below.vue";

export default {
  components: {headline, below},
  data() {
    return {
      title: "子品类管理",
      shwHde: [false, true],
      list: [],
      id: "",
      pageNum: 1,
      pagesize: 10,
    };
  },
  mounted() {
    this.zipinglei();
  },
  methods: {
    handleSizeChange: function (size) {
      this.pagesize = size;
    },
    handleCurrentChange: function (pageNum) {
      this.pageNum = pageNum;
    },
    //子品类数据
    zipinglei() {
      let postData = this.qs.stringify({categoryId: this.$route.params.id});
      this.axios({
        method: "POST",
        url: "/api/manage/category/get_category.do",
        data: postData,
      }).then((res) => {
        this.list = res.data.data;
        this.id = this.$route.params.id;
      });
    },
    //修改姓名
    xiugai(row) {
      this.$prompt("请输入新的品类名称", "提示", {confirmButtonText: "确定", cancelButtonText: "取消"})
          .then(({value}) => {
            let postData = this.qs.stringify({
              categoryId: row.id,
              categoryName: value,
            });
            this.axios({
              method: "POST",
              url: "/api/manage/category/set_category_name.do",
              data: postData,
            }).then((res) => {
              this.zipinglei();
              this.$message({type: "success", message: res.data.data});
            });
          }).catch(() => {
        this.$message({type: "info", message: "取消输入"});
      });
    },
    tianjia() {
      this.$router.push({path: "addgory"});
    },
    // 返回按钮
    fanh() {
      this.$router.push({path: "/home/category"});
    },
  },
};
</script>
<style scoped lang="less">
h4 {
  padding-left: 15px;
  padding-top: 10px;
}

.block {
  margin-top: 15px;
  background: white;
  margin-bottom: 10px;
}
</style>